using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Diagnostics;
using System.Windows.Forms;

using Janus.Windows.Schedule;
namespace Janus.NorthwindDemo2007
{
	public partial class AppointmentsView
	{

		private void FillOwners()
		{
			this.Schedule.Owners.Clear();
			DataTable employees = NorthwindApp.NorthwindApplication.GetEmployeeList();

			foreach (DataRow row in employees.Rows)
			{
				this.Schedule.Owners.Add((int)row["EmployeeID"], (string)row["Name"]);
			}
			//now hide all owners except the first one
            for (int i = 1; i < this.Schedule.Owners.Count; i++)
			{
				this.Schedule.Owners[i].Visible = false;
			}
		}
		private void commandManager_CommandClick(object sender, Janus.Windows.UI.CommandBars.CommandEventArgs e)
		{
			
		}
		private void Schedule_ViewChanged(object sender, EventArgs e)
		{

			this.rcmdDayView.Checked= (this.Schedule.View == Janus.Windows.Schedule.ScheduleView.DayView);
			this.rcmdWorkWeek.Checked= (this.Schedule.View == Janus.Windows.Schedule.ScheduleView.WorkWeek);
			this.rcmdWeekView.Checked = (this.Schedule.View == Janus.Windows.Schedule.ScheduleView.WeekView);
			this.rcmdMonthView.Checked = (this.Schedule.View == Janus.Windows.Schedule.ScheduleView.MonthView);

		}

		public override void Edit()
		{
            foreach (ScheduleAppointment appointment in this.Schedule.SelectedAppointments)
			{
                if (appointment.IsRecurring)
                {
                    RecurrenceOptionDialog frm = new RecurrenceOptionDialog();
                    frm.ShowRecurrenceOptionDialog(appointment.Text, false);
                    if (!(frm.DialogResult == DialogResult.OK))
                    {
                        return;
                    }
                    if (frm.ApplyToTheSeries)
                    {
                        this.CreateNewAppointment(appointment.MasterAppointment.StartTime, appointment.MasterAppointment.Duration, appointment.MasterAppointment);
                    }
                    else
                    {
                        this.CreateNewAppointment(appointment.StartTime, appointment.Duration, appointment);
                    }
                }
                else
                {
                    this.CreateNewAppointment(appointment.StartTime, appointment.Duration, appointment);
                }
			}
		}

		private void ShowPageSetup()
		{
			PageSetup form = new PageSetup();
			form.schedulePrintDocument = this.printDocument;
			form.ShowDialog();
			form.Dispose();
		}
		public void CreateNewAppointment(System.DateTime appDate, TimeSpan duration, ScheduleAppointment appointment)
		{
			Cursor = System.Windows.Forms.Cursors.WaitCursor;
			AppointmentsForm form = new AppointmentsForm();
			form.CreateAppointmentForm(this.Schedule, appDate, duration, appointment);
			Cursor = System.Windows.Forms.Cursors.Default;
		}

		private void Schedule_DoubleClick(object sender, System.EventArgs e)
		{
			System.DateTime appDate = this.Schedule.GetDateAt();
			TimeSpan time = this.Schedule.GetTimeAt();
			TimeSpan duration = new TimeSpan(0, this.Schedule.MinuteInterval, 0);
			this.CreateNewAppointment(appDate.Add(time), duration, null);

		}
		private void Schedule_AppointmentDoubleClick(object sender, Janus.Windows.Schedule.AppointmentEventArgs e)
		{
			this.Edit();
		}
		public void AddNew()
		{
			System.DateTime appDate = DateTime.Now;
			if (appDate.TimeOfDay.Minutes >= 30)
			{
				appDate = new System.DateTime(appDate.Year, appDate.Month, appDate.Day, appDate.Hour, 30, 0);
			}
			else
			{
				appDate = new System.DateTime(appDate.Year, appDate.Month, appDate.Day, appDate.Hour, 0, 0);
			}
			TimeSpan duration = new TimeSpan(0, 30, 0);
			this.CreateNewAppointment(appDate, duration, null);

		}

		protected override void OnDataProviderChanged()
		{
			base.OnDataProviderChanged();
			this.FillOwners();
		}

		private void AppointmentsView_Load(object sender, System.EventArgs e)
		{
			this.Schedule.Date = DateTime.Today;
			this.CreateFormatConditions();
		}

		private void CreateFormatConditions()
		{
			ScheduleField field = this.Schedule.Fields["AppointmentCategoryID"];
			ScheduleFormatStyle categoryFormatStyle = new ScheduleFormatStyle();

			ScheduleFormatCondition condition = new ScheduleFormatCondition(field, ConditionOperator.Equal, 1);
			condition.ImageIndex1 = 0;
            condition.FormatStyle.BackColor = Color.FromArgb(128,Color.White); //Color.FromArgb(236, 237, 238);
            condition.FormatStyle.BackColorGradient = Color.FromArgb(128,Color.Tomato);
			condition.FormatStyle.ForeColor = Color.DarkRed;
            condition.BorderColor = Color.DarkRed;
			this.Schedule.FormatConditions.Add(condition);

			condition = new ScheduleFormatCondition(field, ConditionOperator.Equal, 2);
			condition.ImageIndex1 = 1;
            condition.FormatStyle.BackColor = Color.FromArgb(128, Color.White);
            condition.FormatStyle.BackColorGradient = Color.FromArgb(128, Color.OliveDrab);
            condition.FormatStyle.ForeColor = Color.DarkOliveGreen;
			condition.BorderColor = Color.DarkOliveGreen;
			this.Schedule.FormatConditions.Add(condition);

			condition = new ScheduleFormatCondition(field, ConditionOperator.Equal, 3);
			condition.ImageIndex1 = 2;
            condition.FormatStyle.BackColor = Color.FromArgb(128, Color.White);
            condition.FormatStyle.BackColorGradient = Color.FromArgb(128, Color.Gold);
            condition.FormatStyle.ForeColor = Color.Brown;
            condition.BorderColor = Color.Brown;
			this.Schedule.FormatConditions.Add(condition);

			condition = new ScheduleFormatCondition(field, ConditionOperator.Equal, 4);
			condition.ImageIndex1 = 3;
            condition.FormatStyle.BackColor = Color.FromArgb(128, Color.White);
            condition.FormatStyle.BackColorGradient = Color.FromArgb(128, Color.SteelBlue);
            condition.FormatStyle.ForeColor = Color.Navy;
            condition.BorderColor = Color.Navy;
			this.Schedule.FormatConditions.Add(condition);

			condition = new ScheduleFormatCondition(field, ConditionOperator.Equal, 5);
			condition.ImageIndex1 = 4;
            condition.FormatStyle.BackColor = Color.FromArgb(255, Color.White);
            condition.FormatStyle.BackColorGradient = Color.FromArgb(255, Color.DarkOrange);
            condition.FormatStyle.ForeColor = Color.Black;
			this.Schedule.FormatConditions.Add(condition);


		}

        private void ribbon1_CommandClick(object sender, Janus.Windows.Ribbon.CommandEventArgs e)
        {
            switch (e.Command.Key)
            {
                case "rcmdDayView":
                    this.Schedule.View = Janus.Windows.Schedule.ScheduleView.DayView;
                    break;
                case "rcmdWorkWeek":
                    this.Schedule.View = Janus.Windows.Schedule.ScheduleView.WorkWeek;
                    break;
                case "rcmdWeekView":
                    this.Schedule.View = Janus.Windows.Schedule.ScheduleView.WeekView;
                    break;
                case "rcmdMonthView":
                    this.Schedule.Date = new System.DateTime(this.Schedule.Date.Year, this.Schedule.Date.Month, 1);
                    this.Schedule.View = Janus.Windows.Schedule.ScheduleView.MonthView;
                    break;
                case "rcmdPageSetup":
                    this.ShowPageSetup();
                    break;
            }
        }

        private void Schedule_DatesChanged(object sender, EventArgs e)
        {
            this.rcmdDayView.Checked = (this.Schedule.View == Janus.Windows.Schedule.ScheduleView.DayView);
            this.rcmdWorkWeek.Checked = (this.Schedule.View == Janus.Windows.Schedule.ScheduleView.WorkWeek);
            this.rcmdWeekView.Checked = (this.Schedule.View == Janus.Windows.Schedule.ScheduleView.WeekView);
            this.rcmdMonthView.Checked = (this.Schedule.View == Janus.Windows.Schedule.ScheduleView.MonthView);

        }
	}

} //end of root namespace